package com.neusoft.yuejian12.yuejian_12.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.activity.PostDetailActivity;
import com.neusoft.yuejian12.yuejian_12.javabean.PostBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.neusoft.yuejian12.yuejian_12.constant.Constant.POST_PHOTO_URL;
import static com.neusoft.yuejian12.yuejian_12.constant.Constant.USER_PHOTO_URL;

/**
 * Created by ZTY on 2017/10/11.
 */

public class PostListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List mDataList;
    private ViewHolder viewHolder;
    private LayoutInflater mLayoutInflaterp;

    public PostListAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflaterp = LayoutInflater.from(mContext);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflaterp.from(parent.getContext())
                .inflate(R.layout.cardview_post, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final PostBean entity = (PostBean) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.posttitle.setText(entity.getP_title());
   //   Log.d("a","posttitle");
        viewHolder.postinfo.setText(entity.getP_info());
        viewHolder.postusername.setText(entity.getU_name());
        String post_userpic = USER_PHOTO_URL+entity.getU_pic();
        String post_pic = POST_PHOTO_URL + entity.getP_pic();
        if (!post_userpic.equals("")){
            Picasso.with(mContext).load(post_userpic).error(R.drawable.ic_add).into(viewHolder.imageuserpic);
        }
        if (!post_pic.equals("")){
            Picasso.with(mContext).load(post_pic).error(R.drawable.ic_add).into(viewHolder.imagepost);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("p_id", entity.getP_id());
                bundle.putString("p_title", entity.getP_title());
                bundle.putString("p_pic", entity.getP_pic());
                bundle.putInt("u_id", entity.getU_id());
                bundle.putString("p_info",entity.getP_info());
                bundle.putString("u_name", entity.getU_name());
                bundle.putString("u_pic", entity.getU_pic());
                Intent intent=new Intent(mContext, PostDetailActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount () {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imagepost;
        TextView posttitle;
     //   TextView posttime;
        ImageView imageuserpic;
        TextView postinfo;
        TextView postusername;

        public ViewHolder(View itemView) {
            super(itemView);
            imagepost = (ImageView) itemView.findViewById(R.id.post_pic);
            posttitle = (TextView) itemView.findViewById(R.id.post_title);
            postinfo = (TextView) itemView.findViewById(R.id.post_info);
            imageuserpic = (ImageView) itemView.findViewById(R.id.post_userpic);
           // posttime = (TextView) itemView.findViewById(R.id.post_createtime);
            postusername = (TextView) itemView.findViewById(R.id.post_username);

        }
    }

}
